package com.andnnl.list;

import java.util.Arrays;
import java.util.List;

/**
 * Created by chenss on 2023/12/28.
 */
public class MatchFirstTest {
    public static void main(String[] args) {
        System.out.println(matchFirst(Arrays.asList("asdf","bbbb","cccc")));
        System.out.println("....................");
        System.out.println(matchFirst2(Arrays.asList("asdf","bbbb","cccc")));
        System.out.println("....................");
        System.out.println(matchFirst3(Arrays.asList("asdf","bbbb","cccc")));
    }

    private static String matchFirst(List<String> tempList) {
        return tempList.stream()
                .filter(temp -> isMatch(temp))
                .peek(temp-> System.out.println("peek->"+temp))
                .map(temp->temp+"...")
                .findFirst()
                .orElse(null);
    }

    private static String matchFirst2(List<String> tempList) {
        return tempList.stream()
                .map(temp->temp+"...")
                .peek(temp-> System.out.println("peek->"+temp))
                .filter(temp -> isMatch(temp))
                .findAny()
                .orElse(null);
    }

    private static String matchFirst3(List<String> tempList) {
        return tempList.stream()
                .map(temp->getTemp(temp))
                .filter(temp -> temp!=null)
                .peek(temp-> System.out.println("peek->"+temp))//java.lang.NullPointerException
                .findFirst()
                .orElse(null);
    }

    private static String getTemp(String temp) {
        return null;
    }

    private static boolean isMatch(String temp) {
        System.out.println(temp);
        return temp.startsWith("d");
    }
    private static boolean isMatch2(String temp) {
        System.out.println(temp);
        return temp.startsWith("b");
    }
}
